package edu.gdin.ecommerce.dao;

import org.springframework.stereotype.Repository;

import edu.gdin.ecommerce.model.CardBatch;
import edu.gdin.ecommerce.util.Pager;

@Repository("cardBatchDAO")
public class CardBatchDAO extends BaseDAO {

	public Pager queryBatch(CardBatch batch, String operTime1, String operTime2, int currentPage) {
		StringBuffer hql = new StringBuffer();
		hql.append("from CardBatch b where 1=1 ");
		
		if(batch != null) {
			if(batch.getCardIssuer() != null && batch.getCardIssuer().getCardIssuerId() != null 
					&& batch.getCardIssuer().getCardIssuerId() != -1 ) {
				hql.append(" and b.cardIssuer.cardIssuerId = " + batch.getCardIssuer().getCardIssuerId());
			}
			if(batch.getMaterialType() != null && batch.getMaterialType() != -1) {
				hql.append(" and b.materialType = " + batch.getMaterialType());
			}
			if(batch.getStatus() != null && batch.getStatus() != -1) {
				hql.append(" and b.status = " + batch.getStatus());
			}
			if(batch.getBatchNo() != null && !batch.getBatchNo().equals("")) {
				hql.append(" and b.batchNo = " + batch.getBatchNo());
			}
			if(batch.getNum() != null && !batch.getNum().equals("")) {
				hql.append(" and b.num = " + batch.getNum());
			}
			if (batch.getBatchDesc() != null && !batch.getBatchDesc().equals("")) {
				hql.append(" and b.batchDesc like '%" + batch.getBatchDesc() + "%'");
			}
			
		}
		
		if(operTime1 != null && !operTime1.equals("")) {
			hql.append(" and b.createTime >= '" + operTime1 + " 00:00:00'");
		}
		if(operTime2 != null && !operTime2.equals("")) {
			hql.append(" and b.createTime <= '" + operTime2 + " 23:59:59'");
		}
		hql.append(" order by b.createTime desc");
		
		return this.getPage(hql.toString(), currentPage);
	}
	
	
}
